<template>
  <div></div>
</template>

<script>
import { isFunction, isNil, isObject } from "lodash";
export default {
  props: {
    callback: {
      type: Function,
      default: function () { },
    },
    shareData: {
      type: Object,
      default: () => { },
    },
    pageurl: {
      type: String,
      default: '/weChatApp/home',
    },
  },
  data() {
    return {
      wxReadys: false,
      messageShare: false,
      defaultShareData: {
        title: "哲人石",
        desc: "哲人石家长课堂",
        imgUrl: window.location.origin + "/weChatApp/logo.jpg", // 分享图标
      },
      wxShareData: {}
    };
  },
  created() {
    if (process.env.NODE_ENV === 'development') return;
    this.wxReadyInit();
  },
  watch: {
    shareData: {
      deep: true,
      immediate: true,
      handler: function (val, old) {
        if (process.env.NODE_ENV === 'development') return;
        if (!isNil(val) && isObject(val)) {
          this.wxShareData = { ...this.defaultShareData, ...val };
        } else {
          this.wxShareData = this.defaultShareData;
        }
        if(!window.signPageUrl || isNil(window.signPageUrl)){
          window.signPageUrl=window.location.origin+this.pageurl;
        }
        clearTimeout(this.timrs);
        this.timrs = setTimeout(() => {
          if (this.wxReadys) {
            this.shareMethod();
          } else {
            this.wxReadyInit();
          }
        }, 500);
      },
    },
  },
  methods: {
    wxReadyInit() {
      let that = this;
      var u = navigator.userAgent;
      var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
      const toast = this.$toast.loading("加载中...");
      this.$http
        .post(
          this.$API.GET_SIGN_PACKAGE,
          { url: isiOS?window.signPageUrl:window. window.location.href.split("#")[0] },
          {},
          false
        )
        .then((res) => {
          console.log("公众号js绑定", res);
          toast.clear();
          if (res.code == 1) {
            // 配置
            this.$WX.config({
              debug: false,
              appId: res.data.appId,
              timestamp: res.data.timestamp,
              nonceStr: res.data.nonceStr,
              signature: res.data.signature,
              jsApiList: [
                "updateAppMessageShareData",
                "updateTimelineShareData",
                "chooseImage",
                "previewImage",
                "chooseWXPay",
                "getLocation",
                "getLocalImgData",
              ],
            });

            this.$WX.ready(function () {
              console.log("微信网页配置成功");
              that.wxReadys = true;
              that.shareMethod();
              if (isFunction(that.callback)) that.callback();
            });

            this.$WX.error(function (e) {
              console.log("公众号js绑定错误", e);
            });
          }
        })
        .catch((err) => {
          toast.clear();
          console.log("公众号js绑定错误:接口错误", err);
        });


    },
    shareMethod() {
      let shareData = this.wxShareData;
      // 自定义“分享给朋友”及“分享到QQ”按钮的分享内容
      this.$WX.updateAppMessageShareData({
        title: shareData.title, // 分享标题
        desc: shareData.desc, // 分享描述
        link: window.location.href.split("#")[0], // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: shareData.imgUrl, // 分享图标
        success: function (res) {
          console.log("分享给朋友success", res);
        },
      });

      // 自定义“分享到朋友圈”及“分享到QQ空间”按钮的分享内容
      this.$WX.updateTimelineShareData({
        title: shareData.title, // 分享标题
        link: window.location.href.split("#")[0], // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
        imgUrl: shareData.imgUrl, // 分享图标, // 分享图标
        success: function (res) {
          console.log("分享到朋友圈success", res);
        },
      });
    },
  },
};
</script>

<style lang="less">
.not-find-content {
  padding-top: 6%;
  display: flex;
  justify-content: center;
}

.custom-image {
  width: 500px;
  height: auto;
}
</style>